<?php

/**
* CardDealer class
* The CardDealer contains functionality to create a deck of cards, 
* shuffle the deck and deal the cards to the players
* @author dmq (master@dmq.se)
*/
class CardDealer
{
	// creates a new deck
	private $deck = array();

	/**
	* function buildDeck
	* contains the functionality to build a deck with various card types and is controlled by the number of players.
	* @param $players Players objects contains the players who will play a specific game.
	*/
	private function buildDeck( $players )
	{
		$playerOffset = 2;

		// count the number of players
		$nPlayers = count( $players );

		// specifies how many cards of each type of card it will depend on the number of players
		$move3 = 1 * ( $nPlayers + $playerOffset ); // 4
		$move2 = 2 * ( $nPlayers + $playerOffset ); // 8
		$move1 = 3 * ( $nPlayers + $playerOffset ); // 12
		$backUp = 1 * ( $nPlayers + $playerOffset ); // 4
		$turnLeftAndTurnRight = 3 * ( $nPlayers + $playerOffset ); // 12
		$uTurn = 1 * ( $nPlayers + $playerOffset ); // 4

		$cardIndex = 0;


		// while loops add each card in all types in to the deck
		while ( $uTurn > 0 )
		{
			$this->deck[] = $cardIndex++;
			$uTurn--;
		}

		while ( $turnLeftAndTurnRight > 0 )
		{
			$this->deck[] = $cardIndex++;
			$this->deck[] = $cardIndex++;
			$turnLeftAndTurnRight--;
		}

		while ( $backUp > 0 )
		{
			$this->deck[] = $cardIndex++;
			$backUp--;
		}

		while ( $move1 > 0 )
		{
			$this->deck[] = $cardIndex++;
			$move1--;
		}

		while ( $move2 > 0 )
		{
			$this->deck[] = $cardIndex++;
			$move2--;
		}

		while ( $move3 > 0 )
		{
			$this->deck[] = $cardIndex++;
			$move3--;
		}

		// add locked registers to each player
		foreach ( $players as $player )
		{
			foreach ( explode( ",", $player->robot()->lockedRegisters() ) as $card )
			{
				if ( !empty( $card ) )
				{
					array_splice( $this->deck, $card, 1 );
				}
			}
		}
	}

	/**
	* function shuffleDeck
	* contains the functionality of mixing of the created deck.
	*/
	private function shuffleDeck()
	{
		for ( $i = 0; $i < count( $this->deck ); $i++ )
		{	
			// the temporary card
			$tempCard = $this->deck[$i];

			// mixing the card
			$randCard = mt_rand( 0, count($this->deck) - 1 );

			// replacing the card against a mixed card.
			$this->deck[$i] = $this->deck[$randCard];
			$this->deck[$randCard] = $tempCard;
		}
	}

	/**
	* function dealCards
	* contains the functionality to deal the cards to each player from the deck.
	* @param $players Players objects contains the players who will play a specific game.
	*/
	public function dealCards( $players )
	{
		$playerCards = array();

		// creates a deck
		$this->buildDeck( $players );
		
		// mixing the deck
		$this->shuffleDeck();

		foreach ( $players as $player )
		{
			$cards = '';
			for ( $i = 1; $i <= $player->robot()->health() - 1; $i++ )
			{
				$cards .= array_shift( $this->deck );

				if ( $i < $player->robot()->health() - 1)
				{
					$cards .= ",";
				}
			}

			$playerCards[$player->id()] = $cards;
		}

		return $playerCards;
	}
}
